package com.software.utils;
import java.sql.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbutils.ResultSetHandler;

public class JDBCUtils {
    private static BasicDataSource dataSource;

    static {
        try (InputStream input = JDBCUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties")) {
            Properties properties = new Properties();
            if (input != null) {
                properties.load(input);
            }

            dataSource = new BasicDataSource();
            dataSource.setDriverClassName(properties.getProperty("dbcp.driverClassName"));
            dataSource.setUrl(properties.getProperty("dbcp.url"));
            dataSource.setUsername(properties.getProperty("dbcp.username"));
            dataSource.setPassword(properties.getProperty("dbcp.password"));
            dataSource.setInitialSize(Integer.parseInt(properties.getProperty("dbcp.initialSize")));
            dataSource.setMaxTotal(Integer.parseInt(properties.getProperty("dbcp.maxTotal")));
            dataSource.setMaxIdle(Integer.parseInt(properties.getProperty("dbcp.maxIdle")));
            dataSource.setMinIdle(Integer.parseInt(properties.getProperty("dbcp.minIdle")));
            dataSource.setMaxWaitMillis(Long.parseLong(properties.getProperty("dbcp.maxWaitMillis")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取数据库连接
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    // 关闭数据库连接和资源
    public static void release(Statement stmt, Connection conn) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void release(ResultSet rs, Statement stmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        release(stmt, conn);
    }

    // 执行单个更新操作（添加、修改、删除）
    public static int executeUpdate(String sql, Object... params) throws SQLException {
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            return pstmt.executeUpdate();
        }
    }

    // 执行批量更新操作（批量添加、修改、删除）
    public static int[] executeBatch(String sql, Object[][] params) throws SQLException {
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            for (Object[] paramSet : params) {
                for (int i = 0; i < paramSet.length; i++) {
                    pstmt.setObject(i + 1, paramSet[i]);
                }
                pstmt.addBatch();
            }
            return pstmt.executeBatch();
        }
    }

    // 通用的查询操作
    public static <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            try (ResultSet rs = pstmt.executeQuery()) {
                return rsh.handle(rs);
            }
        }
    }
}

